home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11854 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: solon.com!not-for-mail
  2. From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: const pointer confusion...
  5. Date: 26 Mar 1996 19:00:15 -0600
  6. Organization: Los Alamos National Laboratory
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4ja3uv$pf9@solutions.solon.com>
  10. References: <4j06gm$7oa@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4j06gm$7oa@solutions.solon.com>
  14. "Reed R. Mangino" <mangino@planet.net> writes:
  15.  
  16. RM: Could someone please straighten me out on this:
  17. RM: 
  18. RM: 1) const int *p = 10;
  19.  
  20. If this compiles without warning, your compiler is not standard
  21. conforming. Let us forget the =0 for now.
  22.  
  23. RM:     p is a constant pointer to an int, right? While p can be made to
  24. RM:     point to something else, *p can never be assigned to, right?
  25.  
  26. Almost correct. First, what you describe is named pointer to const
  27. int: a const pointer to int is what you describe in (2) below.
  28.  
  29. Second, *p cannot be assigned to, but it may be possible to assign to
  30. the same object otherwise. Thus,
  31.  
  32. int x; /* x is modifiable */
  33. const int *p;
  34.  
  35. p = &x; /* a pointer to const int _can_ point at non-const int, other
  36.            way round is dangerous. */
  37. /* *p = 3  is an error */
  38. x = 3; /* this is not an error */
  39.  
  40. RM: 
  41. RM: 2) int *const p;
  42. RM:     p is a pointer to an integer.  *p can be assigned to, but p can
  43. RM:     never be made to point to another address in memory, right?
  44.  
  45. And as you don't initialize it either, it either is a null pointer (if
  46. that declaration appears outside any function), or has indeterminate
  47. value. So, do not try to use *p at all :-)
  48.  
  49. RM: 
  50. RM: 3) int const *p;
  51. RM:     What the heck is this?  I can't find anything like this in my 
  52. RM:     books, but my compiler thinks everything is hunky doory!???
  53.  
  54. const qualifies the preceding thing, except when at the beginning. So,
  55. int const *p is the same as const int *p.
  56.  
  57. Cheers
  58. Tanmoy
  59. --
  60. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  61. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  62. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  63. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  64. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  65. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  66.